module canvas.floatcanvas3d;

import std.stdio;
import utils.vector3;
import utils.color;

class FloatCanvas3D {
    Vector3i mRes;
    RGBColor[] mData;

    this(Vector3i res) {
        mRes = res;

        mData = new RGBColor[res.x * res.y * res.z];
        clearCanvas(RGBColor(0,0,0));
    }

    ///set pixel at pos to color col
    public void drawPixel(Vector3i pos, RGBColor col) {
        mData[pos.z*mRes.x*mRes.y + pos.y*mRes.x + pos.x] = col;
    }

    ///draw pixel, checking bounding box
    public void drawSafePixel(Vector3i pos, RGBColor col) {
        if (pos.x >= 0 && pos.x < mRes.x && pos.y >= 0 && pos.y < mRes.y &&
          pos.z >= 0 && pos.z < mRes.z)
        {
            drawPixel(pos, col);
        }
    }

    ///get color of pixel at pos
    public RGBColor getPixelColor(Vector3i pos) {
        RGBColor ret;
        getPixelColor(pos,ret);
        return ret;
    }

    public void getPixelColor(Vector3i pos, out RGBColor col) {
        col = mData[pos.z*mRes.x*mRes.y + pos.y*mRes.x + pos.x];
    }

    public void clearCanvas(RGBColor col) {
        for (int i = 0; i < mRes.x * mRes.y * mRes.z; i++) {
            mData[i] = col;
        }
    }

    ///change size and allocate new buffer
    public void resizeCanvas(Vector3i newRes) {
        mRes = newRes;
        mData.length = mRes.x * mRes.y * mRes.z;
        writefln("Resizing FloatCanvas3D to %d x %d x %d",mRes.x, mRes.y, mRes.z);
    }

    public Vector3i size() {
        return mRes;
    }

    public int width() {
        return mRes.x;
    }

    public int height() {
        return mRes.y;
    }

    public int depth() {
        return mRes.z;
    }
}
